package calorie.com.calorie.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ModifyPhoneContact;
import calorie.com.calorie.response.ResultDTO;
import retrofit2.Response;

public class ActivityModifyPhoneSecond extends MvpActivity<ModifyPhoneContact.ModifyPhoneContactPresent> implements ModifyPhoneContact.IModifyPhone {
    @BindView(R.id.ed_new_phone)
    EditText ed_new_phone;
    @BindView(R.id.tv_get_code)
    TextView tv_get_code;
    @BindView(R.id.yzm_two)
    EditText yzm_two;


    @Override
    public void getYZM(Response<ResultDTO> response) {

    }

    @Override
    public void getCheckResult(Response<ResultDTO> response) {

    }

    @Override
    public void getYZMSecond(Response<ResultDTO> response) {
        if (response.body().getCode().equals("200")) {
            ToastUtils.showLongToast(context, "已发送验证码");
            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tv_get_code, 60000, 1000);
            countDownTimerUtils.start();
        }
    }

    @Override
    public void getModifyResult(Response<ResultDTO> response) {
        if (response.body().getCode().equals("200")) {

            Map<String,Object> map = ( Map<String,Object> )response.body().getData();
            Object mobilePhone = map.get("mobilePhone");
            if(mobilePhone!=null){
                ToastUtils.showLongToast(context, "修改成功");
                SPUtils.putString(Constant.PHONE, map.get("mobilePhone").toString(), context);
                this.finish();
            }

        } else if (response.body().getCode().equals("201")) {
            ToastUtils.showLongToast(context, "服务器异常");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected ModifyPhoneContact.ModifyPhoneContactPresent createPresent() {
        return new ModifyPhoneContact.ModifyPhoneContactPresent();
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_phone_two;
    }

    @OnClick({R.id.tv_get_code, R.id.bt_login_submit})
    public void onViewClicked(View view) {
        String phone = ed_new_phone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.tv_get_code://发送验证码
                if (phone.equals("")) {
                    ToastUtils.showLongToast(context, "请输入手机号码");
                    return;
                } else if (!RegexUtils.isMobileExact(phone)) {
                    ToastUtils.showLongToast(context, "请输入正确的手机号码");
                    return;
                }
                mPresent.modifyPhoneThree(phone);
                break;
            case R.id.bt_login_submit:
                String yzm = yzm_two.getText().toString().trim();
                if (phone.equals("")) {
                    ToastUtils.showLongToast(context, "请输入手机号码");
                    return;
                } else if (!RegexUtils.isMobileExact(phone)) {
                    ToastUtils.showLongToast(context, "请输入正确的手机号码");
                    return;
                } else if (yzm.equals("")) {
                    ToastUtils.showLongToast(context, "请输入验证码");
                    return;
                }
                mPresent.modifyPhoneTwo(context, yzm, phone);
                break;
        }

    }
}
